package com.road.vast.springboot;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;

import java.util.List;

/**
 * @Author 郭泽鹏
 * @Date 2020/6/26 11:43
 * @Version 1.0
 * @Description 全部流程实例挂起与激活
 */
public class SuspendProcessInstance {
    public static void main(String[] args) {
        //创建ProcessEngine对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        //创建RepositoryService对象
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();

        //创建ProcessDefinitionQuery查询器
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();

        //设置查询条件
        processDefinitionQuery.processDefinitionKey("holiday4");

        //查询出结果
        List<ProcessDefinition> list = processDefinitionQuery.list();

        //遍历查询结果
        for (ProcessDefinition processDefinition : list) {
            String processDefinitionId = processDefinition.getId();
            //流程实例是否暂停
            boolean suspended = processDefinition.isSuspended();

            if (suspended){
                //激活流程实例
                repositoryService.activateProcessDefinitionById(processDefinitionId, true, null);
                System.out.println("流程定义ID号" + processDefinitionId + "已激活");
            }else{
                //将流程实例挂起
                repositoryService.suspendProcessDefinitionById(processDefinitionId, true, null);
                System.out.println("流程定义ID号" + processDefinitionId + "已挂起");
            }
        }
    }
}
